package client.gui.panels;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;

import controllers.ControllerIF;
import controllers.TimeFrameApprovalByInspectorController;

public class TimeFrameApprovalByInspectorPanel extends JPanel implements ActionListener, PanelIF {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private TimeFrameApprovalByInspectorController timeFrameApprovalByInspectorController;
	
	/**
	 * local reference for the requested time frame
	 */
	private Integer time;
	
	/**
	 * the inspector decision about duration
	 */
	private Integer inputNum;
	
	private JTextField daysTextField;
	private JRadioButton approvalRadioBtn;
	private JRadioButton resetRadioBtn;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JPanel hiddenPanel;
	private JButton confirmBtn;

	/**
	 * Create the panel.
	 */
	public TimeFrameApprovalByInspectorPanel(Integer time, String stage) {
		this.time = time;
		initGUI(time, stage);
	}
	
	/**
	 * Create the GUI
	 * @param time Integer
	 * @param stage String
	 */
	public void initGUI(Integer time, String stage) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createVerticalStrut(20));
		
		JPanel promptPanel = new JPanel();
		add(promptPanel);
		promptPanel.setLayout(new BoxLayout(promptPanel, BoxLayout.X_AXIS));
		
		promptPanel.add(Box.createHorizontalGlue());
		
		JLabel titleLabel = new JLabel("Requested time frame of " + time.toString() + " days for " + stage + " stage.");
		promptPanel.add(titleLabel);
		
		promptPanel.add(Box.createHorizontalGlue());
		
		add(Box.createVerticalStrut(20));
		
		JPanel actionPanel = new JPanel();
		add(actionPanel);
		actionPanel.setLayout(new BoxLayout(actionPanel, BoxLayout.Y_AXIS));
		
		JPanel panel = new JPanel();
		actionPanel.add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		
		panel.add(Box.createHorizontalStrut(20));
		
		JLabel promptLabel = new JLabel("Please choose an action:");
		panel.add(promptLabel);
		
		panel.add(Box.createHorizontalGlue());
		
		JPanel buttonsPanel = new JPanel();
		actionPanel.add(buttonsPanel);
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
		
		JPanel approvalPanel = new JPanel();
		buttonsPanel.add(approvalPanel);
		approvalPanel.setLayout(new BoxLayout(approvalPanel, BoxLayout.X_AXIS));
		
		approvalPanel.add(Box.createHorizontalStrut(20));
		
		approvalRadioBtn = new JRadioButton("Approve time duration");
		buttonGroup.add(approvalRadioBtn);
		approvalPanel.add(approvalRadioBtn);
		approvalRadioBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
		approvalRadioBtn.addActionListener(this);
		approvalRadioBtn.setToolTipText("Click this button to approve the time duration request for that stage");
		
		approvalPanel.add(Box.createHorizontalGlue());
		
		JPanel rejectPanel = new JPanel();
		buttonsPanel.add(rejectPanel);
		rejectPanel.setLayout(new BoxLayout(rejectPanel, BoxLayout.X_AXIS));
		
		rejectPanel.add(Box.createHorizontalStrut(20));
		
		resetRadioBtn = new JRadioButton("Set different time duration");
		rejectPanel.add(resetRadioBtn);
		buttonGroup.add(resetRadioBtn);
		resetRadioBtn.setAlignmentX(Component.RIGHT_ALIGNMENT);
		resetRadioBtn.addActionListener(this);
		resetRadioBtn.setToolTipText("Click this button to reset a new time duration for that stage");
		
		rejectPanel.add(Box.createHorizontalGlue());
		
		hiddenPanel = new JPanel();
		buttonsPanel.add(hiddenPanel);
		hiddenPanel.setLayout(new BoxLayout(hiddenPanel, BoxLayout.X_AXIS));
		hiddenPanel.setVisible(false);
		
		JLabel resetPromptLabel = new JLabel("Time duration given for " + stage + " stage:");
		hiddenPanel.add(resetPromptLabel);
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		hiddenPanel.add(horizontalStrut);
		
		daysTextField = new JTextField();
		hiddenPanel.add(daysTextField);
		daysTextField.setMaximumSize(new Dimension(40, 16));
		daysTextField.setColumns(3);
		
		JLabel lblDays = new JLabel(" days.");
		hiddenPanel.add(lblDays);
		
		buttonsPanel.add(Box.createVerticalStrut(20));
		
		JPanel confirmPanel = new JPanel();
		buttonsPanel.add(confirmPanel);
		confirmPanel.setLayout(new BoxLayout(confirmPanel, BoxLayout.X_AXIS));
		
		confirmPanel.add(Box.createHorizontalGlue());
		
		confirmBtn = new JButton("Confirm");
		confirmPanel.add(confirmBtn);
		confirmBtn.addActionListener(this);
		
		confirmPanel.add(Box.createHorizontalStrut(20));
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getSource().equals(resetRadioBtn)) {
			hiddenPanel.setVisible(true);
		}
		else if(ae.getSource().equals(approvalRadioBtn)) {
			hiddenPanel.setVisible(false);
		}
		else if(ae.getSource().equals(confirmBtn)) {
			inputNum = null;
			if(resetRadioBtn.isSelected()) {
				inputNum = null;
			}
			else if(resetRadioBtn.isSelected()) {
				try {
					inputNum = Integer.parseInt(daysTextField.getText());
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(this, "Error", "Failed to turn " + daysTextField.getText() + " to integet type", JOptionPane.ERROR_MESSAGE);
				}
				// In case the inspector inserted the same amount of days as the evaluator asked for 
				if(inputNum.equals(time)) {
					inputNum = null;
					JOptionPane.showMessageDialog(this, "Relax", "You have entered the exact time duration as the evaluator ask for. Don't worry, I got your back on this.", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			// TODO : send to server the inputNum variable
			timeFrameApprovalByInspectorController.action();
		}
	}
	
	public Integer getInspectorDecision() { 
		return inputNum;
	}

	@Override
	public void setController(ControllerIF cif) {
		timeFrameApprovalByInspectorController = (TimeFrameApprovalByInspectorController) cif;
	}

	/**
	 * Set all text areas and buttons editable / uneditable according to a given parameter
	 * @param bool boolean
	 */
	public void setPanelEditable(Boolean bool) {
		approvalRadioBtn.setEnabled(bool);
		resetRadioBtn.setEnabled(bool);
	}
}
